//define(['mui','vue'],function(mui,Vue){
! function() {
	var dragImg,
		Data = {},
		city = [];
	var vm = new Vue({
		el: '#main',
		data: {
			citycode: '101130101',
			cans: false,
			distrct: '',
			street: '',
			temperature: "",
			direction: '',
			speed: '',
			feelsLike: '',
			humidity: '',
			quality: '',
			today: '',
			tomorrow: '',
			acquired: '',
			aqi: '',
			todayd: '',
			tomorrowd: '',
			acquiredd: '',
			desc: '',
			clothesimg: '',
			spfimg: '',
			clothes: '',
			spf: '',
			clothesdesc: '',
			spfdesc: '',
			sport: '',
			sportdesc: '',
			sportimg: '',
			drive: '',
			drivedesc: '',
			driveimg: ''
		},
		mounted: function() {
			this.ajax(101130101)
		},
		methods: {
			getcode: function(p) {
				vm.distrct = p.address.district;
				vm.street = p.address.street;
				city.push(p.address.city);
				city.push(p.address.district);
				if(city) {
					this.ajax(this.eachCity(city))
				}
			},
			eachCity: function(city) {
				for(var i = 0, c; c = citys[i++];) {
					if(citys[i].countyname.indexOf(city[1])) {
						return citys[i].areaid;
						if(citys[i].countyname.indexOf(city[0])) {
							return citys[i].areaid;
						}
					}
				}
			},
			ajax: function(citycode) {
				mui.ajax('http://zhwnlapi.etouch.cn/Ecalender/api/v2/weather?citykey=' + citycode, {
					dataType: 'json',
					type: 'get',
					success: function(data) {
						//console.log(data);
						var forecast = data.forecast15;
						var indexes = data.indexes;
						vm.quality = data.evn.quality;
						vm.today = forecast[1];
						vm.tomorrow = forecast[2];
						vm.acquired = forecast[3];
						vm.todayd = forecast[1].day;
						vm.tomorrowd = forecast[2].day;
						vm.acquiredd = forecast[3].day;
						vm.clothesimg = indexes[1].ext.icon;
						vm.clothesdesc = indexes[1].desc;
						vm.clothes = indexes[1].valueV2;
						vm.spfimg = indexes[4].ext.icon;
						vm.spfdesc = indexes[4].desc;
						vm.spf = indexes[4].valueV2;
						vm.sportimg = indexes[5].ext.icon;
						vm.sportdesc = indexes[5].desc;
						vm.sport = indexes[5].valueV2;
						vm.driveimg = indexes[6].ext.icon;
						vm.drivedesc = indexes[6].desc;
						vm.drive = indexes[6].valueV2;
						Data.data1 = data;
					}
				})
				mui.ajax('https://weatherapi.market.xiaomi.com/wtr-v3/weather/all?latitude=110&longitude=112&locationKey=weathercn%3A' + citycode + '&days=15&appKey=weather20151024&sign=zUFJoAR2ZVrDy1vF3D07&isGlobal=false&locale=zh_cn', {
					dataType: 'json',
					type: 'get',
					success: function(data) {
						//console.log(data);
						var current = data['current'];
						vm.aqi = data.aqi.aqi;
						vm.temperature = current['temperature'];
						vm.direction = current['wind']['direction'];
						vm.speed = current['wind']['speed'];
						vm.feelsLike = current['feelsLike'];
						vm.humidity = current['humidity'];
						vm.desc = data.forecastHourly.desc;
						Data.data2 = data;
						vm.dragImg();
					}
				})
			},
			dragImg: function() {
				if(!this.cans) {
					return this.cans = true;
				}
				//console.log(Data)
				var wthrs = [],
					wd = [],
					wp = [],
					time = [],
					shidu = [];
				for(var i = 0, w; w = Data.data1.hourfc[i++];) {
					wthrs.push(w.wthr);
					time.push(w.time.slice(8, 10));
					shidu.push(w.shidu);
				}
				for(var i = 0, w; w = Data.data2.forecastHourly.wind.value[i++];) {
					wd.push(w.derection);
					wp.push(w.speed);
				}
				//console.log(time);
				var canv = document.getElementById('foo'),
					ctx = canv.getContext('2d');
				//设置原点
				ctx.translate(80, 120);
				//设置字体
				ctx.font = "24px serif";
				//开始
				ctx.beginPath();
				//起点
				ctx.moveTo(0, -wthrs[0]);
				//文本位置
				ctx.fillText(wthrs[0] + '℃', -20, -wthrs[0] - 16);
				ctx.fillText(wp[0], -20, 150);
				//绘制圆形
				ctx.arc(0, -wthrs[0], 4, 0, 2 * Math.PI);
				//获取所有点
				for(var i = 0, c = wthrs.length; i < c; i++) {
					ctx.lineWidth = 3;
					ctx.lineTo(i * 120, -wthrs[i]);
					//填充字体颜色
					ctx.fillStyle = "#000";
					ctx.fillText(wthrs[i] + '℃', i * 120 - 20, -wthrs[i] - 16);
					ctx.fillText(wp[i], i * 120 - 20, 150);
					ctx.fillText(time[i] + ':00', i * 120 - 22, 250);
					ctx.fillStyle = "#1aa094";
					ctx.fillRect(i * 120 - 37, 160, 80, 30)
					ctx.fillStyle = "#fff";
					ctx.fillText(shidu[i], i * 120 - 20, 185);
					//填充小圆颜色
					ctx.fillStyle = "#288aed";
					ctx.arc(i * 120, -wthrs[i], 4, 0, 2 * Math.PI);

				}
				//线条颜色
				ctx.strokeStyle = '#288aed';
				ctx.stroke();
				ctx.fill();
				//   		    ctx.save();
				ctx.beginPath();
				ctx.lineWidth = 2;
				ctx.strokeStyle = "#000";
				ctx.moveTo(-40, 210);
				ctx.lineTo(2800, 210);
				ctx.stroke();

				for(var i = 0, c = wthrs.length; i < c; i++) {
					ctx.beginPath();
					ctx.lineWidth = 2;
					ctx.strokeStyle = "#000";
					ctx.moveTo(i * 120 + 8, 210);
					ctx.lineTo(i * 120 + 8, 215);
					ctx.stroke();
				}
			},
			broadcast: function() {
				event.stopPropagation();
				var audio = document.querySelector(".audio");
				if(!audio) {
					var audio = new Audio();
					audio.src = 'vedio/music.mp3';
					audio.className = 'audio';
					document.body.appendChild(audio);
					audio.autoplay = "autoplay";
				}
			},
			removebroadcast: function() {
				var audio = document.querySelector(".audio");
				if(audio) {
					document.body.removeChild(audio);
				}
			}
		}
	})
	mui.plusReady(function() {
//		mui.init({
//			pullRefresh: {
//				container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
//				down: {
//					height: 50, //可选,默认50.触发下拉刷新拖动距离,
//					auto: true, //可选,默认false.首次加载自动下拉刷新一次
//					contentdown: "下拉可以刷新", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
//					contentover: "释放立即刷新", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
//					contentrefresh: "正在刷新...", //可选，正在刷新状态时，下拉刷新控件上显示的标题内容
//					callback: vm.ajax() //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
//				}
//			}
//		});
		//获取地址
		plus.geolocation.getCurrentPosition(function(p) {
			vm.getcode(p)
		}, function(e) {
			alert('Geolocation error: ' + e.message);
		});
		document.body.onclick = function() {
			vm.removebroadcast();
		}
		//分享天气
		mui('.mui-table-view').on('tap', '.mui-popover-share', function() {
			plus.share.sendWithSystem({
				content: '分享内容',
				href: 'http://www.dcloud.io/'
			}, function() {
				console.log('分享成功');
			}, function(e) {
				console.log('分享失败：' + JSON.stringify(e));
			});
		})

	})
}();
//})